-- UIChannelSelect
-- Created by chengb Oct/14/2015
-- Android下的渠道选择窗口

UIChannelSelect = class("UIChannelSelect", function()
    return cc.Layer:create();
end);

function UIChannelSelect.create(isForbidGuest, bindedAccounts)
    return UIChannelSelect.new(isForbidGuest, bindedAccounts);
end

-- 按钮点击频率
local CLICK_CD_TIME = 0.3;

-- 构造函数
function UIChannelSelect:ctor(isForbidGuest, bindedAccounts)
    self:setName("UIChannelSelect");

    -- 是否禁用游客登录
    self.isForbidGuest = isForbidGuest;

    -- 当前已绑定的账号
    self.bindedAccounts = bindedAccounts;

    -- 初始化
    local node = cc.CSLoader:createNode("layout/login/android/ChannelSelect.csb");
    self:addChild(node);
    self.node = node;

    -- 注册点击事件
    self:registerTouchEvent();

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIChannelSelect:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIChannelSelect:redraw()
    local titleLabel = findChildByName(self.node, "CT/title");
    titleLabel:setTextColor(TextStyleM.TEXT_COLOR_BLUE);
    titleLabel:setFontSize(TextStyleM.TEXT_SIZE_MIDDLE2);
    titleLabel:setString(getLocStr("sing_in_title"));

    -- Google+登录
    local btnGoogle = findChildByName(self.node, "CT/btn_google");

    -- 亚马逊登录
    local btnAmazon = findChildByName(self.node, "CT/btn_amazon");
    titleLabel:getVirtualRenderer():setAdditionalKerning(kerning);

    local btnGuest = findChildByName(self.node, "CT/btn_guest");
    local btnFacebook = findChildByName(self.node, "CT/btn_facebook");
    local bg = findChildByName(self.node, "CT/bg");

    -- 根据运营平台显示不同的按钮
    if PUBLISH_PLATFORM == PUBLISH_TYPE_GOOGLE_PLAY then
        btnAmazon:setVisible(false);

        -- 如果是APK的版本，或者当前禁用游客登录，则需要屏蔽游客登录
        if isGoogleAPKPackage() or self.isForbidGuest then
            btnGuest:setVisible(false);
            local offset = 60;
            btnGoogle:setPositionY(btnGoogle:getPositionY() + offset);
            btnFacebook:setPositionY(btnFacebook:getPositionY() + offset);
        end

    elseif PUBLISH_PLATFORM == PUBLISH_TYPE_AMAZON then
        btnGoogle:setVisible(false);
        if self.isForbidGuest then
            btnGuest:setVisible(false);
            local offset = 60;
            btnAmazon:setPositionY(btnAmazon:getPositionY() + offset);
            btnFacebook:setPositionY(btnFacebook:getPositionY() + offset);
        end
    end

    -- 如果当前有绑定账号，则只允许使用绑定的渠道登录，其他渠道按钮需要置灰
    if type(self.bindedAccounts) == "table" and #self.bindedAccounts > 0 then
        local bindedChannels = {};
        for _, account in ipairs(self.bindedAccounts) do
            local index = string.find(account, "_");
            local channel = string.sub(account, 1, index - 1);
            table.insert(bindedChannels, channel);
        end

        if PUBLISH_PLATFORM == PUBLISH_TYPE_GOOGLE_PLAY then
            if table.indexOf(bindedChannels, CHANNEL_GOOGLE) == -1 then
                setButtonEnabled(btnGoogle, false);
            end

            if table.indexOf(bindedChannels, CHANNEL_FACEBOOK_GP) == -1 then
                setButtonEnabled(btnFacebook, false);
            end

        elseif PUBLISH_PLATFORM == PUBLISH_TYPE_AMAZON then
            if table.indexOf(bindedChannels, CHANNEL_AMAZON) == -1 then
                setButtonEnabled(btnAmazon, false);
            end

            if table.indexOf(bindedChannels, CHANNEL_FACEBOOK_AM) == -1 then
                setButtonEnabled(btnFacebook, false);
            end
        end
    end
end

-- 进入通信显示模式
function UIChannelSelect:enterCommunicateMode()
    -- 关闭当前界面
    closeFormByName("UIChannelSelect");

    local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
    if uiLogin then
        uiLogin:enterCommunicateMode();
    end
end

-- 注册按钮点击事件
function UIChannelSelect:registerTouchEvent()
    -- 注册游客登录按钮点击事件
    local btnGuest = findChildByName(self.node, "CT/btn_guest");
    local function onGuestClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 限制点击频率
            setClickFrequency(sender, CLICK_CD_TIME);

            -- 尝试进行游客登录
            self:tryGuestLogin();
        end
    end
    btnGuest:addTouchEventListener(onGuestClick);

    -- 注册Google登录按钮点击事件
    local btnGoogle = findChildByName(self.node, "CT/btn_google");
    local function onGoogleClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 限制点击频率
            setClickFrequency(sender, CLICK_CD_TIME);

            local channel = CHANNEL_GOOGLE;
            self:responseChannelClick(channel);
        end
    end
    btnGoogle:addTouchEventListener(onGoogleClick);

    -- 注册Facebook登录按钮点击事件
    local btnFacebook = findChildByName(self.node, "CT/btn_facebook");
    local function onFacebookClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 限制点击频率
            setClickFrequency(sender, CLICK_CD_TIME);

            local channel = getFacebookChannelName();
            self:responseChannelClick(channel);
        end
    end
    btnFacebook:addTouchEventListener(onFacebookClick);

    -- 注册亚马逊登录按钮点击事件
    local btnAmazon = findChildByName(self.node, "CT/btn_amazon");
    local function onAmazonClick(sender, eventType)
       if eventType == ccui.TouchEventType.ended then
           -- 限制点击频率
           setClickFrequency(sender, CLICK_CD_TIME);

           local channel = CHANNEL_AMAZON;
           self:responseChannelClick(channel);
       end
    end
    btnAmazon:addTouchEventListener(onAmazonClick);

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    btnBack:setPositionY(-DESIGN_HEIGHT);
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIChannelSelect");
        end
    end
    AddTouchEventListener(btnBack, onBackClick);
    AddBackEventHandler(self, btnBack, btnBack.touchEventListener);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 点击背景底图关闭
    local bgImg = findChildByName(self.node, "bg/bgImg");
    bgImg:setTouchEnabled(true);
    bgImg:addTouchEventListener(onBackClick);
    local bg2Img = findChildByName(self.node, "CT/bg");
    bg2Img:setTouchEnabled(true);
    bg2Img:addTouchEventListener(onBackClick);
end

-- 响应第三方账号按钮点击
function UIChannelSelect:responseChannelClick(channel)
    -- 如果已经登录了第三方账号，直接请求服务器进行认证
    if hasLogin3rdPlatform(channel) then
        auth3rdPlatform(channel);

        -- 进入通信显示
        self:enterCommunicateMode();
        return;
    end

    -- 打上登录请求标识
    local path = string.format("request_%s_login", channel);
    _G[path] = true;

    -- 显示第三方账号登录窗口
    show3rdAccountLogin(channel);
end

-- 注册事件处理回调函数
function UIChannelSelect:registerEventCallback()
    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            -- 显示登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            uiLogin:setMaskVisible(true);

        elseif ev == "exit" then
            -- 反注册所有事件
            EventMgr.removeAll("UIChannelSelect");

            -- 隐藏登录界面的遮罩
            local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
            if uiLogin then
                uiLogin:setMaskVisible(false);
            end
        end
    end);
end

-- 尝试进行游客登录
function UIChannelSelect:tryGuestLogin()
    local function okFunc()
        -- 尝试进行游客登录
        if not LoginM.tryGuestLogin() then
            return;
        end

        -- 进入通信显示
        local uiLogin = UIMgr.getCurrentScene():getFormByName("UILogin");
        if uiLogin then
            uiLogin:enterCommunicateMode();
        end
    end

    if getLastAccountChannel() ~= "" then
        -- 已经登录过游戏
        okFunc();
        return;
    end

    -- 从未登录过游戏，需要弹出提示
    confirm2(CONFIRM_TYPE_OK, getLocStr("guest_mode"), getLocStr("guest_mode_tip"), okFunc);
end







